package es.cazadoresmadridejos.beans.controller.advertisement;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

import es.cazadoresmadridejos.beans.manager.advertisement.AdvertisementManager;
import es.cazadoresmadridejos.beans.manager.session.UserPreferences;
import es.cazadoresmadridejos.beans.validator.advertisement.AdvertisementValidator;
import es.cazadoresmadridejos.model.Advertisement;
import es.cazadoresmadridejos.model.Advertisement.AdvertisementType;
import es.cazadoresmadridejos.model.Advertisement.OfferType;
import es.cazadoresmadridejos.propertyeditor.AdvertisementPropertyEditor;
import es.cazadoresmadridejos.util.FileUtils;
import es.cazadoresmadridejos.util.FileUtils.FileType;
import es.cazadoresmadridejos.util.validation.ValidationContainer;
import es.cazadoresmadridejos.web.util.ListUtil;


@Controller
public class AdvertisementCreateModifyController {

	@Autowired private AdvertisementManager advertisementManager;
	@Autowired private AdvertisementValidator advertisementValidator;
	@Autowired private MessageSource messageSource;
	@Autowired(required=false) private UserPreferences userPreferences;
	
	@InitBinder
	public void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Throwable{
		binder.registerCustomEditor(Advertisement.class, new AdvertisementPropertyEditor(advertisementManager));
	}
	
	@RequestMapping(value="/advertisementCreateModify.html",method=RequestMethod.GET)
	public ModelAndView createModify(
			@RequestParam(value="id",required=false) Advertisement advertisement) throws Throwable {
		ModelAndView mv = new ModelAndView();
		if(advertisement == null){
			advertisement = new Advertisement();
		}
		
		mv.addObject("advertisement",advertisement);
		mv.addObject("advertisementTypes",ListUtil.toString(AdvertisementType.values()));
		mv.addObject("offerTypes",ListUtil.toString(OfferType.values()));
		mv.addObject("type","create");
		
		return mv;
	}
	@RequestMapping(value="/advertisementCreateModify.html",method=RequestMethod.POST)
	public ModelAndView submit(
			@ModelAttribute("advertisement") Advertisement advertisement, 
			BindingResult result, 
			SessionStatus status,
			HttpServletRequest request
			) throws Throwable {
		
		ModelAndView mv = new ModelAndView("redirect:index.html");
		mv.addObject("screen"," myAdvertisementList.html");
		
		List<ValidationContainer> errors =  advertisementValidator.validate( advertisement);
		if(errors.isEmpty()){
			mv.addObject("okMessage", "THE_advertisement_WAS_CREATED_SUCCESSFULLY");
			advertisement.setPartner(userPreferences.getPartner());
			advertisement.setDate(new Date());
			
			advertisement.setImage(FileUtils.saveForm(advertisement.getFile(), FileType.ADVERTISEMENT));
			advertisement.setActive(1);
			
			advertisementManager.save(advertisement);
			
		}else{
			mv.addObject("errors",errors);
		}
		
		
		return mv;
	}
}
